﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Pharmacy.Data;
using Pharmacy.Entities;
using Pharmacy.Settings.BusinessLayer;
using Microsoft.Reporting.WebForms;

public partial class Admin_PreviewImportAdjust : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string Id = Request.QueryString["GrnId"].ToString();
            PreviewGrn(Id);
        }
    }
    private void PreviewGrn(string GrnId)
    {
        Grn grn = DataRepository.GrnProvider.GetByGrnId(GrnId);
        DataRepository.GrnProvider.DeepLoad(grn);
        TList<GrnDetails> listgrnDetail = DataRepository.GrnDetailsProvider.GetByGrnId(GrnId);
        //DataRepository.GrnDetailsProvider.DeepLoad(grnDetail);
        //ServiceFacade.SettingsHelper.DnCompanyAddress = "167A Nam Kỳ Khởi Nghĩa St., Dist 3, HCMC, Tel:(84 8) 38230498";
        //ServiceFacade.SettingsHelper.DnBrandName = "UNITE FORCE PHARMA 1";
        //ServiceFacade.SettingsHelper.DnVoidTitleEng = "VOID DELIVERY NOTE";
        //ServiceFacade.SettingsHelper.DnVoidTitleVn = "(Hủy phiếu giao hàng)";
        DSGrn.GrnDataTable dtGrn = new DSGrn.GrnDataTable();
        DSGrn.GrnRow dtGrnRow = dtGrn.NewGrnRow();
        dtGrnRow.GrnId = GrnId;
        dtGrnRow.Grndate = grn.GrnDate;
        dtGrnRow.PoId = grn.PoId;
        dtGrnRow.SupplierName = grn.SupplierIdSource.SupplierName;
        dtGrnRow.SupplierAddress = grn.SupplierIdSource.Address;
        dtGrnRow.Phone = grn.SupplierIdSource.Phone;
        dtGrnRow.PrepareBy = grn.PreparedBySource.Name;
        dtGrnRow.Amount = grn.Amount;
        dtGrnRow.BrandName = ServiceFacade.SettingsHelper.DnBrandName;
        dtGrnRow.CompanyAddress = ServiceFacade.SettingsHelper.DnCompanyAddress;

        dtGrn.AddGrnRow(dtGrnRow);

        DSGrn.GrnDetailsDataTable dtGrnDetails = new DSGrn.GrnDetailsDataTable();
        DSGrn.GrnDetailsRow dtGrnDetailsRow;
        foreach (GrnDetails GrnDetailItem in listgrnDetail)
        {
            DataRepository.GrnDetailsProvider.DeepLoad(GrnDetailItem);
            dtGrnDetailsRow = dtGrnDetails.NewGrnDetailsRow();
            dtGrnDetailsRow.DrugId = GrnDetailItem.DrugId;
            dtGrnDetailsRow.DrugName = GrnDetailItem.DrugIdSource.DrugName;
            dtGrnDetailsRow.Unit = GrnDetailItem.DrugIdSource.Unit;
            dtGrnDetailsRow.Quantity = GrnDetailItem.Quantity;
            dtGrnDetailsRow.UnitPrice = GrnDetailItem.UnitPrice;
            if (GrnDetailItem.ExpDate != null) dtGrnDetailsRow.ExpDate = GrnDetailItem.ExpDate.Value;
            dtGrnDetailsRow.BatchLot = GrnDetailItem.BatchLot;
            dtGrnDetailsRow.Remark = GrnDetailItem.Remark;
            dtGrnDetails.AddGrnDetailsRow(dtGrnDetailsRow);

        }

        ReportViewer1.LocalReport.EnableExternalImages = true;
        ReportViewer1.LocalReport.DataSources.Clear();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSGrn_Grn", dtGrn));
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DSGrn_GrnDetails", dtGrnDetails));
        ReportViewer1.DataBind();
    }
}
